<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
    <a-spin :spinning="false">
      <j-form-container :disabled="false" style="padding-bottom: 30px">
        <a-form-model ref="form" :model="model" slot="detail">
          <a-collapse v-model="activeKey">
            <a-collapse-panel key="1" header="基础信息">
              <a-row :gutter="48">
                <a-col :span="12">
                  <a-form-model-item label="归属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyId_dictText">
                    <a-input disabled v-model="model.companyId_dictText" placeholder="请输入归属公司"></a-input>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-collapse-panel>
            <a-collapse-panel key="2" header="财务审计报告">
              <a-row :gutter="48">
                <a-col :span="24">
                  <a-button @click="handleAddChild" v-has="'FinancialAudit:add'" type="primary" style="margin-bottom: 10px">新增</a-button>
                </a-col>
                <a-col :span="24">
                  <a-table
                    ref="table"
                    bordered
                    size="small"
                    rowKey="key"
                    :scroll="{x:'100%'}"
                    :columns="childColumns"
                    :dataSource="dataSource"
                    :loading="childLoading"
                    :pagination="false"
                  >
                    <template slot="fileSlot" slot-scope="text">
                      <a @click="handlePreviews(text)">{{ text }}</a>
                    </template>
                    <template slot="auditYear" slot-scope="text, record">
                      <a @click="handleDetailnew(record)">{{ text }}</a>
                    </template>
                    <span slot="action" slot-scope="text, record">
                      <a @click="handlePreview(record)">预览</a>
                      <a-divider type="vertical"/>
                      <a @click="handleModal(record)">下载</a>
                      <a-divider type="vertical" v-has="'FinancialAudit:update'"/>
                      <a @click="handleData(record)" v-has="'FinancialAudit:update'">编辑</a>
                    </span>
                  </a-table>
                </a-col>
              </a-row>
            </a-collapse-panel>
          </a-collapse>
        </a-form-model>
      </j-form-container>
      <ErpFinancialReportModal ref="financialReport" @ok="modalhandleOk"></ErpFinancialReportModal>
    </a-spin>
    <div class="drawer-footer">
      <a-button @click="handleCancel" style="margin-bottom: 0;">关闭</a-button>
    </div>
    <watermark ref="watermarks" @ok="downloadOK"/>
  </a-drawer>
</template>

<script>
  import ErpFinancialReportModal from './ErpFinancialReportModal'
  import { getAction } from '@/api/manage'
  import PreviewPdf from '@/utils/previewPdfPublic'
  import { axios } from '@/utils/request'
  import { mapGetters } from 'vuex'
  import watermark from './watermark.vue'
  export default {
    name: 'ErpFinancialAuditModal',
    components: {
      ErpFinancialReportModal,
      watermark
    },
    data () {
      return {
        title:"审计列表",
        width:1000,
        visible: false,
        formDisabled: false,
        model: {},
        labelCol: {},
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        activeKey: ['1', '2'],
        dataSource: [],
        childLoading: false,
        childColumns: [
          {
            title:'审计年份',
            align:"center",
            dataIndex: 'auditYear',
            scopedSlots: { customRender: 'auditYear' },
          },
          {
            title:'注册资金(万元)',
            align:"center",
            dataIndex: 'registeredCapital',
          },
          {
            title:'净资产(万元)',
            align:"center",
            dataIndex: 'netAssets',
          },
          {
            title:'总资产(万元)',
            align:"center",
            dataIndex: 'totalAssets',
          },
          {
            title:'固定资产(原值/净值)(万元)',
            align:"center",
            dataIndex: 'fixedAssets',
          },
          {
            title:'流动资产(万元)',
            align:"center",
            dataIndex: 'currentAssets',
          },
          {
            title:'流动负债(万元)',
            align:"center",
            dataIndex: 'currentLiabilities',
          },
          {
            title:'附件',
            align:"center",
            dataIndex: 'attachments',
            scopedSlots: { customRender: 'fileSlot' },
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            width:150,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ]
      }
    },
    methods: {
      ...mapGetters(["userInfo"]),
      handleDetailnew(record){
        this.$refs.financialReport.edit(record,this.model)
        this.$refs.financialReport.title = '详情'
        this.$refs.financialReport.disableSubmit = true
      },
      handleModal(record){
        if(!record.attachments){
          this.$message.warning('暂无附件！')
          return
        }
        this.$refs.watermarks.visible = true
        this.$refs.watermarks.wValue = record.attachments
      },
      downloadOK(){
        getAction('/api/erp/files/company/erpCompanyFiles/downloadAudit').then(result => {})
      },
      handlePreviews(value){
        let fileType = value.substr(-3)
        let text = `${this.userInfo().orgCodeTxt}-${this.userInfo().realname}-${this.userInfo().workNo}`
        let url = window.sessionStorage.getItem('baseUrl') + '/api/erpmng/sys/common/static/' + value
        axios({
          url: url,
          params: {},
          method:'get' ,
          responseType: 'blob',
        }).then(res=>{
          if(fileType==='pdf'){
            new PreviewPdf({
              blob: res,
              docTitle: fileType,
              isAddWatermark: true, // 是否需要添加水印
              watermark: { // watermark必填 里面可以不填
                type: 'canvas',
                text: text,
              }
            })
          }else{
            this.$message.error('非pdf文件不可查看')
          }
          
        })
      },
      edit (record) {
        this.visible = true
        this.$nextTick(() => {
          this.model = Object.assign({},{
            companyId_dictText: record.fileName,
            companyId: record.id
          })
          this.loadData()
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      // 预览
      handlePreview(record){
        let fileType = record.attachments.substr(-3)
        let text = `${this.userInfo().orgCodeTxt}-${this.userInfo().realname}-${this.userInfo().workNo}`
        let url = window.sessionStorage.getItem('baseUrl') + '/api/erpmng/sys/common/static/' + record.attachments
        axios({
          url: url,
          params: {},
          method:'get' ,
          responseType: 'blob',
        }).then(res=>{
          if(fileType==='pdf'){
            new PreviewPdf({
              blob: res,
              docTitle: fileType,
              isAddWatermark: true, // 是否需要添加水印
              way: 'pre', // 下载或者预览
              watermark: { // watermark必填 里面可以不填
                type: 'canvas',
                text: text,
              }
            })
          }else{
            // location.href = url
            this.$message.error('非pdf文件不可查看')
          }
          
        })
      },
      modalhandleOk(){
        this.loadData()
      },
      loadData() {
        this.childLoading = true
        getAction('/api/erp/files/company/erpCompanyAudit/list',{ companyId: this.model.companyId }).then(res => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
          } else {
            this.dataSource = [];
            this.$message.warning(res.message)
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.childLoading = false
        }).catch(err => {
          this.$message.warning(err.message)
        })
      },
      handleAddChild(){
        this.$refs.financialReport.add(this.model)
        this.$refs.financialReport.title = '新增'
        this.$refs.financialReport.disableSubmit = false
      },
      handleData(record){
        this.$refs.financialReport.edit(record,this.model)
        this.$refs.financialReport.title = '编辑'
        this.$refs.financialReport.disableSubmit = false
      }
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
  .drawer-footer{
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
    z-index: 100;
  }
</style>